package com.jinlongchen.service;

import com.jinlongchen.dao.AlbumDao;
import com.jinlongchen.dao.StoryDao;
import com.jinlongchen.model.Album;


import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;

public class AlbumService {
    private final AlbumDao albumDao = new AlbumDao();
    private final StoryDao storyDao = new StoryDao();

    //新建专辑
    public int save(Connection c, int uid, String name, InputStream coverIS,String cover,String filename) throws SQLException {
        try (OutputStream os = new FileOutputStream(filename)){
            byte[] buf = new byte[1024];

            while(true){
                int len = coverIS.read(buf);
                if (len == -1){
                    break;
                }
                os.write(buf,0,len);
            }
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return albumDao.insert(c,uid,name,cover);
    }

    //获取专辑详情
    public Album get(Connection c,int aid) throws SQLException {
        Album album = albumDao.selectOneByAid(aid,c);
        album.storyList = storyDao.selectListByAid(c,aid);

        return album;
    }
}
